# Estimate the conditional effects of polarization and populism using the V-Party.
# See Appendix I.




#############
# load data #
#############

library(brms)
library(ggplot2)
library(ggpubr)
library(stargazer)

load("PSRM Replication Files/PresidentialElectionsPosts.RData")

colnames(president_df)

all(c("vp_populist_election_average", "polarization", "vp_economic_polarization") %in% colnames(president_df)) # TRUE

length(unique(president_df$country)) # 26
length(unique(president_df$electionname)) # 29
length(unique(president_df$party_country)) # 52

# focus on 15 days before and after the election
day_range <- 15

sub <- president_df[which(president_df$daysinceelection >= -1*day_range
                          & president_df$daysinceelection <= day_range),]

# winners and losers
winners <- sub[which(sub$win == 1),]
losers <- sub[which(sub$win != 1),]

length(unique(winners$electionname[which(!is.na(winners$polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$economic_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$social_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_present))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_dummy))])) # 27

length(unique(losers$electionname[which(!is.na(losers$polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$economic_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$social_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_present))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_dummy))])) # 27




##################
# winners + love #
##################

winner_love1 <- brm(loveprop ~ post*vp_populist_election_average + post*polarization
                    + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                    + Xl + Xr 
                    + (1 + Xl + Xr | party_election),
                    data=winners,
                    warmup=1000, iter=2000, chains=3, seed=1234)
#plot(winner_love1)
#summary(winner_love1)

winner_love2 <- brm(loveprop ~ post*vp_populist_election_average + post*vp_economic_polarization
                    + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                    + Xl + Xr 
                    + (1 + Xl + Xr | party_election),
                    data=winners,
                    warmup=1000, iter=2000, chains=3, seed=1234)
#plot(winner_love2)
#summary(winner_love2)




###################
# winners + angry #
###################

winner_angry1 <- brm(angryprop ~ post*vp_populist_election_average + post*polarization
                     + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                     + Xl + Xr 
                     + (1 + Xl + Xr | party_election),
                     data=winners,
                     warmup=1000, iter=2000, chains=3, seed=1234)
#plot(winner_angry1)
#summary(winner_angry1)

winner_angry2 <- brm(angryprop ~ post*vp_populist_election_average + post*vp_economic_polarization
                     + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                     + Xl + Xr 
                     + (1 + Xl + Xr | party_election),
                     data=winners,
                     warmup=1000, iter=2000, chains=3, seed=1234)
#plot(winner_angry2)
#summary(winner_angry2)




#################
# losers + love #
#################

loser_love1 <- brm(loveprop ~ post*vp_populist_election_average + post*polarization
                   + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                   + Xl + Xr 
                   + (1 + Xl + Xr | party_election),
                   data=losers,
                   warmup=1000, iter=2000, chains=3, seed=1234)
#plot(loser_love1)
#summary(loser_love1)

loser_love2 <- brm(loveprop ~ post*vp_populist_election_average + post*vp_economic_polarization
                   + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                   + Xl + Xr 
                   + (1 + Xl + Xr | party_election),
                   data=losers,
                   warmup=1000, iter=2000, chains=3, seed=1234)
#plot(loser_love2)
#summary(loser_love2)




##################
# losers + angry #
##################

loser_angry1 <- brm(angryprop ~ post*vp_populist_election_average + post*polarization
                    + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                    + Xl + Xr 
                    + (1 + Xl + Xr | party_election),
                    data=losers,
                    warmup=1000, iter=2000, chains=3, seed=1234)
#plot(loser_angry1)
#summary(loser_angry1)

loser_angry2 <- brm(angryprop ~ post*vp_populist_election_average + post*vp_economic_polarization
                    + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                    + Xl + Xr 
                    + (1 + Xl + Xr | party_election),
                    data=losers,
                    warmup=1000, iter=2000, chains=3, seed=1234)
#plot(loser_angry2)
#summary(loser_angry2)

#save(winner_love1, winner_love2,
#     winner_angry1, winner_angry2,
#     loser_love1, loser_love2,
#     loser_angry1, loser_angry2,
#     file="Stan_VParty.RData")




##################
# result summary #
##################

load("PSRM Replication Files/Stan_VParty.RData")

summary(winner_love1)
summary(winner_angry1)
summary(loser_love1)
summary(loser_angry1)

summary(winner_love2)
summary(winner_angry2)
summary(loser_love2)
summary(loser_angry2)

# plot interaction terms
getStdPostCI <- function(model, var1, var2, intr, label){
  coeftab <- summary(model)[[14]][,c(1,3,4)]
  coeftab <- coeftab[intr,]
  
  temp <- data.frame(label=label,
                     est=coeftab[1],
                     lwr=coeftab[2],
                     upr=coeftab[3])
  colnames(temp) <- c("label", "est", "lwr", "upr")
  return(temp)
}


# models with average populism (v-party) and polarization (gps)
citab1 <- rbind(getStdPostCI(winner_love1, 2, 3, 12, "Winner + Love"),
                getStdPostCI(winner_love1, 2, 4, 13, "Winner + Love"),
                getStdPostCI(winner_angry1, 2, 3, 12, "Winner + Angry"),
                getStdPostCI(winner_angry1, 2, 4, 13, "Winner + Angry"),
                getStdPostCI(loser_love1, 2, 3, 12, "Loser + Love"),
                getStdPostCI(loser_love1, 2, 4, 13, "Loser + Love"),
                getStdPostCI(loser_angry1, 2, 3, 12, "Loser + Angry"),
                getStdPostCI(loser_angry1, 2, 4, 13, "Loser + Angry"))

citab1$intr <- rep(c("(a) Post Election × Average Populism (V-Party)", 
                     "(b) Post Election × Polarization (GPS)"),
                   times=4)

citab1$label <- factor(citab1$label, levels=rev(unique(citab1$label)))
citab1$intr <- factor(citab1$intr, levels=unique(citab1$intr))

# figure I.1
ggplot(data = citab1, 
       aes(x = est, y = label)) + 
  facet_wrap(. ~ intr, ncol = 2, scales = "free_x") +
  geom_errorbar(data = citab1, aes(xmin = lwr, xmax = upr),
                lwd=0.8, width = 0.1) +
  geom_vline(xintercept=0, lwd=0.5, lty=2) +
  geom_point(cex=4, pch=19) +
  xlab("Posterior Mean and 95% Credible Interval") +
  ylab("") +
  theme_bw() +
  theme(axis.title.x = element_text(size = 15),
        axis.title.y = element_text(size = 15),
        axis.text=element_text(size=12),
        panel.spacing = unit(1, "lines"))

#ggsave("interaction_vp1.pdf", width=12, height=6)


# models with average populism (v-party) and economic polarization (v-party)
citab2 <- rbind(getStdPostCI(winner_love2, 2, 3, 12, "Winner + Love"),
                getStdPostCI(winner_love2, 2, 4, 13, "Winner + Love"),
                getStdPostCI(winner_angry2, 2, 3, 12, "Winner + Angry"),
                getStdPostCI(winner_angry2, 2, 4, 13, "Winner + Angry"),
                getStdPostCI(loser_love2, 2, 3, 12, "Loser + Love"),
                getStdPostCI(loser_love2, 2, 4, 13, "Loser + Love"),
                getStdPostCI(loser_angry2, 2, 3, 12, "Loser + Angry"),
                getStdPostCI(loser_angry2, 2, 4, 13, "Loser + Angry"))

citab2$intr <- rep(c("(a) Post Election × Average Populism (V-Party)", 
                    "(b) Post Election × Economic Polarization (V-Party)"),
                   times=4)

citab2$label <- factor(citab2$label, levels=rev(unique(citab2$label)))
citab2$intr <- factor(citab2$intr, levels=unique(citab2$intr))

# figure I.2
ggplot(data = citab2, 
       aes(x = est, y = label)) + 
  facet_wrap(. ~ intr, ncol = 2, scales = "free_x") +
  geom_errorbar(data = citab2, aes(xmin = lwr, xmax = upr),
                lwd=0.8, width = 0.1) +
  geom_vline(xintercept=0, lwd=0.5, lty=2) +
  geom_point(cex=4, pch=19) +
  xlab("Posterior Mean and 95% Credible Interval") +
  ylab("") +
  theme_bw() +
  theme(axis.title.x = element_text(size = 15),
        axis.title.y = element_text(size = 15),
        axis.text=element_text(size=12),
        panel.spacing = unit(1, "lines"))

#ggsave("interaction_vp2.pdf", width=12, height=6)
